<?php

require_once("Accountant.class.php");
require_once("exceptions/AppExceptions.class.php");

class MainFileStateAccountant extends Accountant {

	private $detail = false;
	/**
	 * 未完成的材料代码的数量的数组
	 *
	 * @var array
	 */
	private $unFinishedCount = array();

	/**
	 * 完成的材料代码的数量的数组
	 *
	 * @var array
	 */
	private $finishedCount = array();

	/**
	 * 文件中distinct的材料代码的数量的数组
	 *
	 * @var array
	 */
	private $allCount = array();

	/**
	 * 上传时间的数组
	 *
	 * @var array
	 */
	private $uploadTime = array();

	/**
	 * 文件名的数组
	 *
	 * @var array
	 */
	private $filename = array();


	/**
	 * 构造函数
	 *
	 * @param boolean $_detail
	 */
	public function __construct($_detail = true){
		$this->detail = $_detail;
		$this->sql = "select filename, uploadTime, count(distinct orderNumber, orderSubitemNumber) as allCount,sum(isFinished) as finishedCount
		from
	(
	select orderNumber, orderSubitemNumber, filename, uploadTime
	from little_main3
	group by orderNumber, orderSubitemNumber, filename
	)
	as main3Table

left outer join

	(
	select orderNumber, orderSubitemNumber,(sumCount <= coalesce(mainCount,0)) as isFinished
	from 
	(
		(select orderNumber, orderSubitemNumber,sum(`count`) as sumCount
		from little_main3
		group by orderNumber,orderSubitemNumber)  as sumCountTable
		left join
		(select orderNumber, orderSubitemNumber,sum(`count`) as mainCount
		from little_main
		group by orderNumber, orderSubitemNumber) as mainCountTable
		using (orderNumber, orderSubitemNumber)
	)
	group by orderNumber, orderSubitemNumber
	)as mainTable
using (orderNumber, orderSubitemNumber)
group by filename
order by uploadTime desc;";
	}

	/**
	 * 执行查询命令，并整理查询的结果
	 *
	 */
	public function execute(){
		$this->result = DB::query($this->sql);
		if($this->result == false){
			return;
		}
		if($this->detail){
			while($row = $this->result->fetch_assoc()){
				array_push($this->filename, $row['filename']);
				array_push($this->uploadTime, $row['uploadTime']);
				array_push($this->allCount, intval($row['allCount']) );
				array_push($this->finishedCount, intval($row['finishedCount']) );
				array_push($this->unFinishedCount, intval($row['allCount']) - intval($row['finishedCount']) );
			}
		}else{
			while($row = $this->result->fetch_assoc()){
				array_push($this->filename, $row['filename']);
				array_push($this->uploadTime, $row['uploadTime']);
				array_push($this->allCount, intval($row['allCount']) );
			}
		}
	}


	public function getFilename(){
		return $this->filename;
	}
	public function getUploadTime(){
		return $this->uploadTime;
	}
	public function getAllCount(){
		return $this->allCount;
	}
	public function getFinishedCount(){
		return $this->finishedCount;
	}
	public function getUnFinishedCount(){
		return $this->unFinishedCount;
	}


}
?>